package org.teremail.store.paged;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

public class MemoryBlobFactory implements BlobFactory {

    private static final int PAGE_SIZE = 1024;
    ConcurrentHashMap<String,List<byte[]>> blobs = 
        new ConcurrentHashMap<String,List<byte[]>>();
    
    volatile long id = 1;
    
    public MemoryBlob createBlob() {
        String id = String.valueOf(++this.id);
        
        
        List<byte[]> pages = new ArrayList<byte[]>();
        
        MemoryBlob b = new MemoryBlob(id, PAGE_SIZE, pages);
        blobs.put(b.getId(), new ArrayList<byte[]>());
        return b;
    }

    public Blob getBlob(String id) {
        List<byte[]> pages = blobs.get(id);
        return new MemoryBlob(id, PAGE_SIZE, pages);
    }

    public void init() {
        // No-op
    }

    public void start() throws Exception {
    }

    public void stop() throws Exception {
    }
}
